<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/5/12
 */

namespace app\classes\model;


use think\Model;

class StudentLeaveModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 获取请假列表信息
     * @param array $filter
     * @param array $order
     * @param int $limit
     * @param $query
     * @return \think\Paginator
     */
    public function getLeave($filter=[], $order=[], $limit=10, $query)
    {
        //默认条件
        $where = [
            'sl.create_time' => ['>=', 0],
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        //默认排序
        $sort = ['sl.id'=>'desc'];
        if(!empty($order) && is_array($order)){
            $sort = array_merge($order,$sort);
        }

        //分页url额外参数
        $page_query['query'] = [];
        if(!empty($query) && is_array($query)){
            $page_query['query'] = $query;
        }

        $data = $this->alias('sl')
            ->field('sl.*,sc.name as campus_name,cl.name as class_name,co.course_name,co.course_hour,
            st.name as student_name,u.realname as teacher_name,sad.id as adjust_id,sad.type as adjust_type')
            ->join('school sc','sl.school_id=sc.id')
            ->join('user u','sl.teacher_id=u.id')
            ->join('student st','sl.student_id=st.id')
            ->join('class cl','sl.class_id=cl.id')
            ->join('course co','sl.course_id=co.id')
            ->join('student_adjust sad','sl.id=sad.leave_id','left')
            ->where($where)
            ->order($sort)
            ->paginate($limit,false,$page_query);

        return $data;
    }
}